﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Deeper.DataAccess
{
	//public class MsSql2kPage : ISqlPage
	//{
	//    public DataTable GetPageDatatableByCustom(int pageIndex, int pageSize, string strSql = null, string where = null, string orderby = null, IHDatabase db = null, params object[] param)
	//    {

	//        string sql;

	//        if (where.IsNotNull())
	//        {
	//            strSql = string.Format("({0} where {1}) as tp", strSql, where);
	//            where = "";
	//        }
	//        else strSql = string.Format("( {0} ) as tp", strSql);
	//        string field = "*";
	//        if (pageIndex == 1)
	//        {
	//            if (where.IsNull())
	//                sql = string.Format("select top {0} {1} from {2} order by {3}  ", pageSize, field, strSql, orderby);
	//            else
	//                sql = string.Format("select top {0} {1} from {2} where {3} order by {4} ", pageSize, field, strSql, where, orderby);
	//        }
	//        else
	//        {
	//            pageIndex = (pageIndex - 1) * pageSize;

	//            sql = string.Format("select top {0} {1} from {2} where {3} > all(select top {4} {3} from {2} order by {3} ) order by {3}  ", pageSize, field, strSql, orderby, pageIndex);

	//        }

	//        IHDatabase databsae = db ?? new SqlDatabase();
	//        DataSet ds = (param.IsNull() || param.Count() == 0) ? databsae.GetDataset(sql) : databsae.GetDataset(sql, param);
	//        if (ds.Tables.Count > 0) return ds.Tables[0];
	//        return null;
	//    }

	//}

	//public class Mssql2005Page : ISqlPage
	//{
	//    public DataTable GetPageDatatableByCustom(int pageIndex, int pageSize, string strSql = null, string where = null, string orderby = null, IHDatabase db = null, params object[] param)
	//    {
	//        string sql;
	//        IHDatabase databsae = db ?? new SqlDatabase();

	//        if (strSql.IsNull())
	//        {
	//            HTableAttribute currentTableInfo = DatabaseScheme.GetTableInfo<T>();
	//            strSql = currentTableInfo.sqlSelect;
	//        }
	//        int pos = strSql.IndexOf(" from");
	//        sql = strSql.Insert(pos, string.Format(",ROW_NUMBER() OVER(order by {0}) as rnuserdpage", orderby));
	//        if (!where.IsNull()) sql = string.Format("{0} where {1}", sql, where);
	//        sql = string.Format("select * from ({0}) tmptablepaged where rnuserdpage between {1} and {2}", sql, (pageIndex - 1) * pageSize + 1, pageSize * pageIndex);
	//        DataSet ds = where.IsNull() ? databsae.GetDataset(sql) : databsae.GetDataset(sql, param);
	//        if (ds.Tables.Count > 0) return ds.Tables[0];
	//        return null;
	//    }
	//}
}
